title: “Data analysis - Deficit as % of GDP” output: html_document —
Uploading data
df_deficit_pct <- read_csv("data/deficit_pct_gdp_OECD.csv")
Creating a comparators variable
comparators <- c("DNK","IRL","NZL","NOR","SWE")
df_deficit_pct <- df_deficit_pct %>%
mutate(in_comparators=if_else(LOCATION%in%comparators,TRUE,FALSE))
Creating a dataframe that starts in 1995 (when we start having data for all countries)
df_deficit_1995on <- df_deficit_pct %>%
filter(TIME>1995)
Renaming deficit_pc to balance of payments
df_deficit_1995on <- df_deficit_1995on %>%
mutate(balance_pc = deficit_pc)
Transforming deficit_pc (currently a measure of balance of payments where positive values mean surpluses) into a measure of deficit (higher values mean higher deficits)
df_deficit_1995on <- df_deficit_1995on %>%
mutate(deficit_pc = -deficit_pc)
Creating rolling averages over 3 and 5 years These variable names will be changed as now they are no longer rolling averages of the balance but of actual deficit
df_deficit_1995on <- df_deficit_1995on %>% #this function uses longform
group_by(LOCATION) %>%
mutate(def_03ya = rollmean(deficit_pc, k = 3, fill = NA),
def_05ya = rollmean(deficit_pc, k = 5, fill = NA)) %>%
ungroup() #this closes that group_by(LOCATION)
Creating different measures of rolling minimum deficits Creating rolling minimums over 3 and 5 years This is the minimum deficit in rolling 5 year periods including GFC
df_deficit_1995on <- df_deficit_1995on %>%
group_by(LOCATION) %>%
mutate(min_05y_all = rollapply(deficit_pc,5,min, fill=NA, align = "right"),
min_03y_all = rollapply(deficit_pc,3,min, fill=NA, align = "right")
) %>%
ungroup ()
Creating a dataframe that does not take the GFC into account
df_deficit_no_GFC <- df_deficit_1995on %>%
filter(!(TIME%in%c(2009,2010,2011,2012,2013)))
Creating rolling minimums over 3 and 5 years This is the minimum deficit in rolling 5 year periods excluding GFC from 1995
df_deficit_no_GFC <- df_deficit_no_GFC %>%
group_by(LOCATION) %>%
mutate(min_05y_no_GFC = rollapply(deficit_pc,5,min, fill=NA, align = "right"),
min_03y_no_GFC = rollapply(deficit_pc,3,min, fill=NA, align = "right")
) %>%
ungroup ()
Creating a table with maximum values of 5-year rolling minimums with GFC years from 1995
max_5ymin <- df_deficit_1995on %>%
group_by(LOCATION) %>%
arrange(desc(min_05y_all)) %>%
slice(1) %>%
ungroup()
Creating a table with maximum values of 5-year rolling minimums without GFC years from 1995
max_5ymin_no_GFC <- df_deficit_no_GFC %>%
group_by(LOCATION) %>%
arrange(desc(min_05y_no_GFC)) %>%
slice(1) %>%
ungroup()
Creating rolling means of deficit without GFC years
df_deficit_no_GFC <- df_deficit_no_GFC %>%
group_by(LOCATION) %>%
mutate(def_03ya_no_GFC = rollmean(deficit_pc, k = 3, fill = NA),
def_05ya_no_GFC = rollmean(deficit_pc, k = 5, fill = NA)) %>%
ungroup()
Creating a table with maximum values of 5-year rolling average without GFC years from 1995
max_5ya_no_GFC <- df_deficit_no_GFC %>%
group_by(LOCATION) %>%
arrange(desc(def_05ya_no_GFC)) %>%
slice(1) %>%
ungroup()
Creating a table with maximum values of 5-year rolling average with all years (including GFC) from 1995
max_5ya_all <- df_deficit_1995on %>%
group_by(LOCATION) %>%
arrange(desc(def_05ya)) %>%
slice(1) %>%
ungroup()
deficit_pc and its rolling average recodings: Positive values mean budget deficits and negative values mean budget surpluses
Visualising deficit as % of GDP for all countries in all years
p1 <- df_deficit_pct %>%
ggplot(aes(TIME, deficit_pc, colour=LOCATION)) +
geom_line()
ggplotly(p1)
Maximum 5-year minimums for all years (including GFC)
p2 <- max_5ymin %>%
ggplot(aes(LOCATION,min_05y_all, group=TIME)) +
geom_col() +
coord_flip()
ggplotly(p2)
Interpretation:
e.g. Australia: 2.89 is the minimum value recorded between 2008 and 2012. It was recorded in 2012. In 2008, the deficit rose significantly (from -0.72 to 3.75), and started going back down hence the lowest deficit in the 2008 to 2012 5-year period was in 2012 (2.89) and given the high deficits of this period it was the highest minimum deficit that Australia recorded.
The year that appears when you hover is the year in which the value was first considered the minimum 5-year value, not the year in which this deficit occurred
e.g. Germany (see table below): 2005, 3.03. This value occurred in 2001, and in the 5-year period between 2001 and 2005. In 2001, there was a sharp increase in deficit (from 1.58 to 3.03), and the deficit kept growing. Until 2004, the minimum 5-year deficit was from 2000, before this sharp increase. In 2005, the minimum 5-year value is the one for 2001, where the sharp increase happened.
tab_de <- df_deficit_1995on %>%
select(LOCATION, TIME, deficit_pc, min_05y_all) %>%
filter(LOCATION == "DEU" & TIME %in% c(1999,2000,2001,2002,2003,2004,2005,2006))
kbl(x = tab_de) %>%
kable_paper(bootstrap_options = "striped", full_width = F, position = "left")
| LOCATION | TIME | deficit_pc | min_05y_all |
|---|---|---|---|
| DEU | 1999 | 1.719220 | NA |
| DEU | 2000 | 1.584664 | 1.584664 |
| DEU | 2001 | 3.025445 | 1.584664 |
| DEU | 2002 | 3.874993 | 1.584664 |
| DEU | 2003 | 3.704201 | 1.584664 |
| DEU | 2004 | 3.334158 | 1.584664 |
| DEU | 2005 | 3.319437 | 3.025445 |
| DEU | 2006 | 1.653152 | 1.653152 |
Maximum 5-year minimums excluding GFC
p3 <- max_5ymin_no_GFC %>%
ggplot(aes(LOCATION,min_05y_no_GFC, group=TIME)) +
geom_col() +
coord_flip()
ggplotly(p3)
Maximum 5-year averages for all years (including GFC)
p4 <- max_5ya_all %>%
ggplot(aes(LOCATION,def_05ya, group=TIME)) +
geom_col() +
coord_flip()
ggplotly(p4)
Maximum 5-year averages excluding GFC
p5 <- max_5ya_no_GFC %>%
ggplot(aes(LOCATION,def_05ya_no_GFC, group=TIME)) +
geom_col() +
coord_flip()
ggplotly(p5)
Exporting data to CSV